package com.thememaker.lockstyle;

import com.thememake.entity.ThemeFileInfo;
import com.thememaker.lockstyle.bean.ui.BaseView;
import com.thememaker.lockstyle.bean.ui.Image;
import com.thememaker.lockstyle.control.LayoutComponentUtils;
import com.thememaker.lockstyle.parsii.ExpressionUtils;
import com.thememaker.lockstyle.parsii.LockscreenViewPaesiiUtils;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.nio.file.Path;
import java.util.ArrayList;


public class LockStyleCenterEditUtils {
    private static final ArrayList<VBox> views = new ArrayList<>();
    private final Stage stage;
    private ScrollPane rightAnchorPane, rightrightPane;
    private java.nio.file.Path lockscreenPath;
    private ExpressionUtils expressionUtils;
    private ThemeFileInfo themeFileInfo;
    private int screen_height = 1920;
    private int screen_width = 1080;


    private VBox vBox2;
    private LockstyleShowUtils lockstyleShowUtils;

    public int getScreen_height() {
        return screen_height;
    }

    public void setScreen_height(int screen_height) {
        this.screen_height = screen_height;
    }

    public LockStyleCenterEditUtils(ScrollPane rightAnchorPane, ScrollPane rightrightPane, ThemeFileInfo themeFileInfo, Stage stage) {
        this.rightAnchorPane = rightAnchorPane;
        this.rightrightPane = rightrightPane;
        this.themeFileInfo = themeFileInfo;
        this.stage = stage;
        expressionUtils = ExpressionUtils.getInstance(screen_width, screen_height);

        vBox2 = LayoutComponentUtils.getInstance().getControlView(440, 150);
        vBox2.setAlignment(Pos.CENTER_LEFT);
    }

    public void updateRightImageInfo(BaseView baseView, java.nio.file.Path lockscreenPath, LockstyleShowUtils lockstyleShowUtils) {
//        lockscreenPathString = lockscreenPath.toString();
        this.lockscreenPath = lockscreenPath;
        this.lockstyleShowUtils = lockstyleShowUtils;
        System.err.println("lockscreenPathString======================" + lockscreenPath);
        rightrightPane.setContent(vBox2);
        vBox2.setVisible(true);
        vBox2.getChildren().clear();
        if (baseView instanceof Image) {
            Image image = (Image) baseView;
            if (image.getSourcesAnimation() != null && image.getSourcesAnimation().getSourceList() != null
                    && image.getSourcesAnimation().getSourceList().size() > 0) {
                for (int i = 0; i < image.getSourcesAnimation().getSourceList().size() - 1; i++) {
                    Image imgOBJ = new Image();
                    imgOBJ.setSrc(((Image) baseView).getSourcesAnimation().getSourceList().get(i).getSrc());
                    imgOBJ.setX(((Image) baseView).getSourcesAnimation().getSourceList().get(i).getX());
                    imgOBJ.setY(((Image) baseView).getSourcesAnimation().getSourceList().get(i).getY());
                    imgOBJ.setTime(((Image) baseView).getSourcesAnimation().getSourceList().get(i).getTime());
                    vBox2.getChildren().add(initImage(imgOBJ));
                }
            } else {
                vBox2.getChildren().add(initImage(baseView));
            }
        }
    }

    public Node initImage(BaseView baseView) {
        Image imageUi = (Image) baseView;
        Path path = LockscreenViewPaesiiUtils.getInstance(expressionUtils).paesiiImagePath(imageUi, lockscreenPath.toString());
        StackPane stackPane = LayoutComponentUtils.getInstance().getImagePane(lockscreenPath,200, 150, path.toString(), 200, 150, false, path1 -> {
            lockstyleShowUtils.updateRightPreview(baseView);
        });
        Label labelName = LayoutComponentUtils.getInstance().getControlNormalLabel("图片名称：");
        Label labelSize = LayoutComponentUtils.getInstance().getControlNormalLabel("实际尺寸：");
        TextField fieldName = LayoutComponentUtils.getInstance().getControlTextField("", 130, false);
        TextField fieldSize = LayoutComponentUtils.getInstance().getControlTextField("", 130, false);
        HBox hBox1 = new HBox();
        HBox hBox2 = new HBox();
        hBox1.setAlignment(Pos.CENTER_LEFT);
        hBox2.setAlignment(Pos.CENTER_LEFT);
        hBox1.getChildren().addAll(labelName, fieldName);
        hBox2.getChildren().addAll(labelSize, fieldSize);
        HBox imgInfoHbox = new HBox();
        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER_LEFT);
        VBox.setMargin(hBox1, new Insets(0, 0, 5, 10));
        VBox.setMargin(hBox2, new Insets(5, 0, 0, 10));
        vBox.getChildren().addAll(hBox1, hBox2);
        imgInfoHbox.getChildren().addAll(stackPane, vBox);
        fieldName.setText(path.getFileName().toString());
        javafx.scene.image.Image imageFX = new javafx.scene.image.Image("file:" + path);
        fieldSize.setText(imageFX.getWidth() + "*" + imageFX.getHeight());
        return imgInfoHbox;
    }

    public void clear(){
        if (rightrightPane!=null && rightrightPane.getContent()!=null){
            rightrightPane.getContent().setVisible(false);
        }
    }
}

